package source.view;

import java.awt.*;

import javax.swing.*;

import source.*;
import source.Entity.EntityType;

/**
 * Displays the current player's global status as well as the status
 * of the currently selected entity.
 * @author McDonald's Farm
 *
 */
public class StatusViewport extends JPanel {
	private EntityType selectedType;
	private PlayerStatus playerStatus;
	private JPanel playerStats;
	private JPanel selectedStats;
	private JPanel entityHud;
	
	public StatusViewport(){
		this.setLayout(new GridLayout(3,1));
		playerStats = new JPanel();
		selectedStats = new JPanel();
		selectedType = EntityType.UNIT;
		entityHud = new JPanel();
		add(playerStats);
		add(selectedStats);
		add(entityHud);
	}
	/**
	 * Constructor
	 * @param player - reference to the current PlayerStatus
	 */
	public StatusViewport(PlayerStatus player){
		this();
		this.playerStatus = player;
	}
	/**
	 * Sets the EntityType that is currently selected.
	 * @param e
	 */
	public void setSelectedType(EntityType e){
		selectedType = e;
	}
	/**
	 * Set the current PlayerStatus.
	 * @param playerStatus
	 */
	public void setCurrentPlayer(PlayerStatus playerStatus){
		this.playerStatus = playerStatus;
	}
	/**
	 * display a Structure stats
	 */
	public void displayStructure(){
		selectedType = EntityType.STRUCTURE;
		update();
	}
	/**
	 * display unit stats
	 */
	public void displayUnit(){
		selectedType = EntityType.UNIT;
		update();
	}
	/**
	 * Update the current viewport
	 */
	public void update(){
		selectedStats.removeAll();
		playerStats.removeAll();
		entityHud.removeAll();
		
		JLabel label;

		String s;
		switch(selectedType){
		case STRUCTURE:
			Base base = playerStatus.getCurrentStructure();
			String building;
			if(base.beingBuilt() == null) building = "Nothing";
			else building = String.valueOf(base.beingBuilt());
			s = "<html>"
				+ "<table><tr><td>" + base.getStructureType() + "</td><td> </td></tr>"
				+ "<tr><td>HP: </td><td>" + base.getCurrentHP() + "/" + base.getMaxHP() + "</td></tr>"
				+ "<tr><td>Production Rate:</td><td>" + base.getProductionRate() + "</td></tr>" 
				+ "<tr><td>Currently Building:</td><td>" + building + "</td></tr>"
				+ "</table></html>";
			selectedStats.add(new JLabel(s));
			/*label = new JLabel(MainScreen.getUnitIconLookup().get(String.valueOf(playerStatus.getID()) +
					base.getStructureType()));
			entityHud.add(label); */
			break;
		case UNIT:
			Unit unit = playerStatus.getCurrentUnit();
			s = "<html>"
				+ "<table><tr><td>" + unit.getUnitType() + " Selected</td><td> </td></tr>"
				+ "<tr><td>Remaining Moves: </td><td>" + unit.getRemainingMoves() + "</td></tr>"
				+ "<tr><td>HP: </td><td>" + unit.getCurrentHP() + "/ " + unit.getMaxHP() + "</td></tr>"
				+ "<tr><td>Energy Needed: </td><td>" + unit.getUnitType().upkeep() + "</td></tr>"
				//+ "<tr><td>Destination Tile: </td><td>" + unit.getDestinationTile().getX() + ", " + unit.getDestinationTile().getY() + "</td></tr>"
				+ "</table></html>";
			label = new JLabel(s);
			label.setMinimumSize(new Dimension(300,300));
			selectedStats.add(label);
			
			label = new JLabel(MainScreen.getUnitIconLookup().get(String.valueOf(playerStatus.getID()) +
					unit.getUnitType() + unit.getOrientation()));
			entityHud.add(label);
			
			break;
		}
		s = "<html><table>"
			+ "<tr><td>Player " + playerStatus.getID() + " Stats</td><td></td></tr>"
			+ "<tr><td>ORE : </td><td>" + playerStatus.getOre() + "</td</tr>"
			+ "<tr><td>Energy : </td><td>" + playerStatus.getEnergyUsed() + "/" + playerStatus.getEnergy() + "</td></tr>"
			+ "</table></html>";
		label = new JLabel(s);
		label.setMinimumSize(new Dimension(300,300));
		playerStats.add(label);
		
		validate();
		repaint();
	}
}
